import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class DioCase extends StatefulWidget {
  const DioCase({Key? key}) : super(key: key);

  @override
  _DioCaseState createState() => _DioCaseState();
}

class _DioCaseState extends State<DioCase> {
  @override
  void initState() {
    // TODO: implement initState
    getData();
    super.initState();
  }

  List _channels = [];
  getData() async {
    String path = 'https://geek.itheima.net';
    Dio dio = Dio();
    var res = await dio.get('$path/v1_0/channels');
    setState(() {});
    _channels = res.data['data']['channels'] as List;
    // print(res);
  }

  @override
  Widget build(BuildContext context) {
    return GridView(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4, mainAxisSpacing: 10, crossAxisSpacing: 10),
      children: _channels.map((dynamic item) {
        return Container(
          color: Colors.blue,
          alignment: Alignment.center,
          child: Text(
            item['name'],
            style: const TextStyle(color: Colors.white, fontSize: 20),
          ),
        );
      }).toList(),
    );
  }
}
